require "util.util"
require "tests.lunity"
require "util.constants"
module("util", package.seeall, lunity)

ItemOne = 1
ItemTwo = 2
ItemFive = 5
TestList = { ItemOne, ItemTwo }

function test_contains_returns_true_when_list_contains_object()
    assertTrue(contains(TestList, ItemTwo), "List should contain object")
end

function test_compareTo_returns_COMPARETO_FIRST_IS_SMALLER_when_first_parameter_is_smaller_than_second_paramter()
    assertEqual(compareTo(0,1), COMPARETO_FIRST_IS_SMALLER, "compareTo should return COMPARETO_FIRST_IS_SMALLER")
end

function test_compareTo_returns_COMPARETO_FIRST_IS_BIGGER_when_first_parameter_is_bigger_than_second_paramter()
    assertEqual(compareTo(1,0), COMPARETO_FIRST_IS_BIGGER, "compareTo should return COMPARETO_FIRST_IS_BIGGER")
end

function test_compareTo_returns_COMPARETO_FIRST_AND_SECOND_ARE_EQUAL_when_first_parameter_is_equal_to_second_paramter()
    assertEqual(compareTo(1,1), COMPARETO_FIRST_AND_SECOND_ARE_EQUAL, "compareTo should return COMPARETO_FIRST_AND_SECOND_ARE_EQUAL")
end

function test_contains_returns_false_when_list_does_not_contain_object()
    assertFalse(contains(TestList, ItemFive), "List should not contain object")
end

function test_split_breaks_string_into_array_based_on_delimiter()
    delimiter = ","
    numbers = "1"..delimiter.."2"..delimiter.."3"
    fields = Split(numbers,delimiter)
    assertEqual(#fields, 3, "Split should return correct array")
end

function test_iif_returns_second_argument_when_first_argument_is_true()
    testvalue = "foo"
    iif_result = iif(true, testvalue, nil)
    assertEqual(iif_result, testvalue, "iif should have returned correct value")
end

function test_iif_returns_third_argument_when_first_argument_is_false()
    testvalue = "foo"
    iif_result = iif(false, nil, testvalue)
    assertEqual(iif_result, testvalue, "iif should have returned correct value")
end

function test_endswith_returns_false_if_either_parameter_is_nil()
    assertion = "endswith should return false if either parameter is nil"
    assertFalse(endswith(nil, ""), assertion)
    assertFalse(endswith("", nil), assertion)
end

function test_getRandomFrom_returns_unpredictable_result()
    results = {}
    for i=1, 100 do
        randomTeam = getRandomFrom({ TEAM_ONE, TEAM_TWO })
        results[randomTeam] = iif(results[randomTeam] == nil, 0, results[randomTeam]) + 1
    end
    assert(results[TEAM_ONE] > 0 and results[TEAM_TWO] > 0, "getRandomFrom should return unpredictable result")
end

runTests{useANSI = false}